<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="width: 100%">
      <!-- 搜索框，添加商品按钮 -->
      <el-row>
        <el-col :span="8">
          <el-input
            v-model="searchInput"
            placeholder="请输入内容"
            clearable
            @clear="serchOrder()"
            @change="serchOrder()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="serchOrder()"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList" border stripe class="tablefontsize">
        <el-table-column label="#" type="index" align="center" width="40">
        </el-table-column>
        <el-table-column label="订单编号" prop="order_number">
        </el-table-column>
        <el-table-column label="订单价格" prop="order_price" width="80">
        </el-table-column>
        <el-table-column label="是否付款" prop="pay_status" width="80">
          <template v-slot:default="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'"
              >未付款</el-tag
            >
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="80">
        </el-table-column>
        <el-table-column label="下单时间" width="160">
          <template v-slot:default="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <!-- 操作，修改和查看两个按钮 -->
        <el-table-column label="操作" width="120">
          <template v-slot:default="scope">
            <el-button type="primary" @click="editOrder(scope.row)" size="mini">
              <i class="el-icon-edit"> </i>
            </el-button>
            <el-button
              type="success"
              @click="selectWuLiu(scope.row)"
              size="mini"
            >
              <i class="el-icon-position"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页显示条 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 20, 30]"
        background
        :page-size="queryInfo.pagesize"
        :current-page="queryInfo.pagenum"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改物流信息的对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editOrderDialogVisiable"
      width="50%"
      @close="editOrderDialogClose()"
    >
      <el-form
        :model="editOrderForm"
        ref="editOrderFormRef"
        :rules="editOrderFormRules"
        label-width="90px"
        status-icon
        label-position="left"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="citydata"
            v-model="editOrderForm.address1"
            :props="{ expandTrigger: 'hover' }"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editOrderForm.address2"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="editOrderDialogVisiable = false">取 消</el-button>
        <el-button type="primary" @click="editOrderDialogVisiable = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 物流信息的对话框 -->
    <el-dialog
      title="查看物流进度"
      :visible.sync="wuliuDialogVisible"
      width="50%"
    >
      <el-timeline :reverse="false">
        <el-timeline-item
          v-for="(item, index) in wuliuInfo"
          :key="index"
          :timestamp="item.time"
        >
          {{ item.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import citydata from './citydata.js'
export default {
  data () {
    return {
      orderList: [],
      searchInput: '',
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      editOrderDialogVisiable: false,
      wuliuDialogVisible: false,
      editOrderForm: { address1: [], address2: '' },
      editOrderFormRules: {
        address1: [{ required: true, message: '请选择省是区', trigger: 'blur' }],
        address2: [{ required: true, message: '请填写详细的物流地址', trigger: 'blur' }]
      },
      citydata,
      wuliuInfo: []
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    async getOrderList () {
      const { data: res } = await this.$http.get(
        'orders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      } else {
        this.orderList = res.data.goods
        this.total = res.data.total
      }
    },
    serchOrder () {
      this.queryInfo.pagenum = 1
      this.queryInfo.query = this.searchInput
      this.getOrderList()
    },
    // 每页显示数量改变
    handleSizeChange (newPageSize) {
      this.queryInfo.pagesize = newPageSize
      this.getOrderList()
    },
    // 页码改变
    handleCurrentChange (newPageNum) {
      this.queryInfo.pagenum = newPageNum
      this.getOrderList()
    },
    // 修改订单地址
    editOrder (row) {
      this.editOrderDialogVisiable = true
    },
    async selectWuLiu (row) {
      const { data: res } = await this.$http.get('/kuaidi/110675712662')
      if (res.meta.status === 200) {
        this.wuliuInfo = res.data
        this.wuliuDialogVisible = true
      } else {
        this.$message.error('物流信息获取失败' + res.meta.messagef)
      }
    },
    editOrderDialogClose () {
      this.editOrderDialogVisiable = false
      this.$refs.editOrderFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.addGoodsBtn {
  margin-left: 20px;
}
.tablefontsize {
  font-size: 10px;
}
.el-cascader {
  width: 100%;
}
</style>
